package com.example.demo.customer;

import com.example.demo.infoapp.InfoApp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;


@Configuration
public class CustomerConfiguration {

    @Value("${app.useFakeCustomerRepo:false}")      //    注解Value
    private Boolean useFakeCustomerRepo;            //    声明变量useFakeCustomerRepo

    @Value("${info.company.name}")
    private String companyName;

    //注释Autowired让Spring自动装配(字段注入）
    @Autowired
    private Environment environment;

//    在CLR方法中传入InfoApp,并打印出此变量
    @Bean
    CommandLineRunner commandLineRunner(InfoApp infoApp){
        //  return args ->    //  返回lambda值
        return args -> {
            System.out.println("HelloWorld!");
            System.out.println(companyName);
            //这里会输出来自application.properties中先前定义好的info.company.name

            System.out.println(environment.getProperty("info.app.version"));
            System.out.println(infoApp);
        };
    }

//编写customerRepo()方法,根据值返回CFR或者CR的实例
    @Bean
    CustomerRepo customerRepo(){
            System.out.println("useFakeCustomerRepo :"+useFakeCustomerRepo);
            return new CustomerFakeRepository();
//            return useFakeCustomerRepo?new CustomerFakeRepository():new CustomerRepository();
        }
}
